package com.apes.fn.server.transferVehicle.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.fn.crm.customerExpand.FnCustomerExpandRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.pi.service.PiManager;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Creator: zouyc
 * CreateDate: 2020-02-18 0:35
 * Description:
 **/
@Service("deliveryOrderService")
public class DeliveryOrderService extends DomainService {

    @Autowired
    PartyRoleRepository partyRoleRepository;
    @Autowired
    FnCustomerExpandRepository customerExpandRepository;
    @Autowired
    PiManager piManager;

    /***
     * 跨系统外协
     * 1. 生成对应的会员
     * 2. 生成对应的车辆
     */


    public JSONObject acrossSystemCreateVipDetail(SimpleRequest request) {
        // 交车公司
        PartyRole deliveryCompany = partyRoleRepository.findOne((String) request.get("deliveryCompanyId"));

        // 接车公司
        PartyRole receiptCompany = partyRoleRepository.findOne((String) request.get("receiptCompanyId"));

        Map requetMap = MapUtil.mapper(
                "vipxx", createVipxx(deliveryCompany, receiptCompany),
                "vipclxx", createVipclxx(request, receiptCompany)
        );

        return piManager.synErp("ipmVipService.acrossSystemCreateVipDetail",
                this.invoke("fn.deliveryOrder.createVipDetailLimit", requetMap));
    }


    private Map createVipclxx(SimpleRequest request, PartyRole receiptCompany) {
        FnCustomerExpand customerExpand = customerExpandRepository.findOne((String) request.get("customerExpandId"));

        if( customerExpand == null ) throw new RuntimeException("未找到对应的会员拓展表，编码：" + request.get("customerExpandId"));

        return MapUtil.mapper(
                "CPHM", customerExpand.getCarLicense(),
                "CJHM", customerExpand.getVin(),
                "GSID", receiptCompany.getId(),
                "PZCXID", customerExpand.getJekunModelCode()
        );
    }

    private Map createVipxx(PartyRole deliveryCompany, PartyRole receiptCompany) {
        return MapUtil.mapper(
                "VIPXM", deliveryCompany.getParty().getAbbreviation(),
                "SJHM1", deliveryCompany.getParty().getCorporate().getMobileNumber(),
                "GSID", receiptCompany.getId()
        );
    }


}